<html>
    <head>
        <link href="style3.css" rel="stylesheet" type="text/css" />
        <title></title>
       
    </head>
    <?php
    session_start();
    include ("functions.php");
    if (isset($_GET['conference_id'])) { // checks if the conference id is passed from the previous page
        $id = $_SESSION['member_id'];//get the member id by a session variable and save it in a variable
        $confid = intval($_GET['conference_id']); //get the conference id that is passed from the previous page
			//establish a database connection
			
		$done = 0;
        $conn = @mysql_connect("localhost", "root", "") or die(mysql_error());
        mysql_select_db("mydb") or die(mysql_error());
			//get all categories in this conference
        $categories_in_conference = mysql_query("SELECT category, c.category_id FROM category c, conference_category cc WHERE c.category_id = cc.category_id
												AND cc.conference_id = $confid") or die(mysql_error());
			// save number of categories ,that is indicated by the number of rows in the result table, in a variable 
        $numm = mysql_num_rows($categories_in_conference);
        
			// a loop that loops on the number of category rows in th resulted table to insert the category name and a drop down list that contains 
			//values from 1 to 10 in html table 
        while ($row = mysql_fetch_assoc($categories_in_conference)) {
			$category_id = $row['category_id'];

            echo "<table>";// initiate the table
            echo "<form method='post' action='' name='form'>";//make a form with a post method
            echo "<td style='width:100px;'>", $row['category'], "</td>"; // print the name of each category
				// make a hidden input that takes a value the id of this category to keep track of the category id 
            echo "<td>", "<input name='c_id[]' type='hidden' value='{$row['category_id']}'/>", "</td>";
            echo "<td style='width:100px;'>";
            print "<select name=\"Categories[]\" id=\"{$row['category_id']}\" \">"; // create a drop down list to each category
			//Populate it with values from 1 to 10
			echo '<option>' . "select a number" . '</option>';
			echo '<option value="' . 0 . '">' . 0 . '</option>';
            echo '<option value="' . 1 . '">' . 1 . '</option>';
            echo '<option value="' . 2 . '">' . 2 . '</option>';
            echo '<option value="' . 3 . '">' . 3 . '</option>';
            echo '<option value="' . 4 . '">' . 4 . '</option>';
            echo '<option value="' . 5 . '">' . 5 . '</option>';
            echo '<option value="' . 6 . '">' . 6 . '</option>';
            echo '<option value="' . 7 . '">' . 7 . '</option>';
            echo '<option value="' . 8 . '">' . 8 . '</option>';
            echo '<option value="' . 9 . '">' . 9 . '</option>';
            echo '<option value="' . 10 . '">' . 10 . '</option>', "</td>";
        }
        echo "</select></br>", "</tr>";// close drop down list 
        echo "</table>"; // close table 
        echo "<td><p align='center'><input type ='submit' name ='Save' value ='Save' /></p></td>"; // create a Save button to insert the data in the database

        
	
	if (isset($_POST['Save'])) { // check of the save button is clicked 
        if (isset($_POST['Categories'])) { //check if a drop down list is selected 
            for ($i = $numm; $i > 0; $i--) { // loop on the number of categories insert the value that is selected from each drop down list a the database 
                $max = $_POST['Categories'][$i-1]; // save the value of the selected item from the drop down list in a variable
					// save the value of the hidden input which contain the category id of each drop down list to know which category are we inserting into 
                $cat_id = $_POST['c_id'][$i-1];
                $maximum_number = mysql_real_escape_string($max); // sanitizing the varible to insert it in the database
				$category_id = mysql_real_escape_string($cat_id); // sanitizing the varible to insert it in the database

				//if there is no value selected , view an alert
				if ($maximum_number == 0)
						{ 
						echo  '<script type="text/javascript">alert("Please make sure you selected a number in all categories");</script>'; 
						
						break;
						}
						else
						{
					//query to check if this member has already inserted a value in this category before	
				$not_already_set = mysql_query ("SELECT * FROM limit_paper WHERE member_id = '$id' 
												AND conference_id = '$confid' AND category_id = $category_id ") or die(mysql_error());
					// count the rows							
				$count = mysql_num_rows($not_already_set);
						// if the member hasn't already inserted a value in this category before	
					if ($count != 1){
					//insert the member id , conference id , category id and the maximumnumber of papers that the reviewer could accept in a conference in the database
				$maximum_paper_insertions = " INSERT INTO limit_paper(member_id,conference_id,category_id,maximum_papers) VALUES('$id', '$confid','$category_id','$maximum_number') ";
                mysql_query($maximum_paper_insertions) or die(mysql_error());
				//update the done variable as a counter to check later if all categories are inserted
				$done ++; 
				}
					else {
						// if the member didn't insert, view an alert
					echo  '<script type="text/javascript">alert("You have already set a limit for this category");</script>';
					break;
					}
					}
				
					
							
			}
			//check if all the drop down lists are selected, an alert should appear to make sure they're all selected
			//check later if the couter of insertions is equal to the number of categories in the conference
				if ($done == $numm)
				{  
					echo "<script type='text/javascript'>alert('done');</script>";
				}
        }
			
				
    }
}
     echo "</form>";
    ?>

</html>

